package tjs.study.notes.dotest.disruptor.apiLog.config;

import com.alibaba.fastjson.JSONObject;
import com.lmax.disruptor.RingBuffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import tjs.study.notes.dotest.disruptor.parallel.spring.DisruptorNotice;
@Component
@ConditionalOnProperty(name = "shsnc.project.disruptor.enable", matchIfMissing = false, havingValue = "true")
public class DisruptorProducer {

    @Autowired
    private RingBuffer<DisruptorNotice> ringBuffer;

    public DisruptorProducer(RingBuffer<DisruptorNotice> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void sendJson(Object noticeJson) {
        long sequence = ringBuffer.next();
        try {
            DisruptorNotice disruptorNotice = ringBuffer.get(sequence);
            disruptorNotice.setNoticeJson(JSONObject.toJSONString(noticeJson));
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}